//
//  InputServerPage.swift
//  FlameB
//
//  Created by 连俊 on 2020/8/5.
//  Copyright © 2020 连俊. All rights reserved.
//

import UIKit

class InputServerPage: BaseViewController {
    var curPageIndex:Int = 1
    lazy var tableView: UITableView = {
        let tab = UITableView(frame: .zero, style: UITableView.Style.plain)
        tab.separatorStyle = .none
        return tab
    }()
    
    var dataSource: [String] = [
        "100m",
        "1500m",
        "5km越野",
        "25km越野",
        "50km越野"
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func buildView() {
        navigationItem.title = "项目录入"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(navigationBarHeight)
            make.left.right.bottom.equalToSuperview()
        }
        tableView.register(HomeSportCell.self, forCellReuseIdentifier: HomeSportCell.reuseid)
        tableView.rowHeight = HomeSportCell.CellHeight
        tableView.estimatedRowHeight = HomeSportCell.CellHeight
        tableView.backgroundColor = UIColor.init(rgb: 0xF7F7F7)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        tableView.delaysContentTouches = false
        
        setHeadAndFooterRefresh()
    }
    
    private var isFirstAppear: Bool = true
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if isFirstAppear{
            isFirstAppear = false
            tableView.mj_header?.beginRefreshing()
        }
    }
}

extension InputServerPage: UITableViewRereshProtocol {
    func startLoadPage(with index: Int) {
        tableView.reloadData()
        refreshUI(with: index, isHasNextPage: false, hiddenFooter: true)
    }
}

extension InputServerPage: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeSportCell.reuseid) as! HomeSportCell
        cell.titleString = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.row {
        case 0:
            let vc = InputServerDetailPage()
            navigationController?.pushViewController(vc, animated: true)
        default:
            break
        }
    }
}
